package com.hunan.api.catv.mq.consumer;

import com.google.common.reflect.TypeToken;
import com.hunan.api.catv.common.Constants;
import com.hunan.api.catv.entity.vo.MemberVo;
import com.hunan.api.catv.mq.MqConstants;
import com.hunan.api.catv.service.biz.toupiao.TouPiaoBiz;
import com.hunan.api.catv.utils.GsonUtils;
import com.hunan.api.catv.utils.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Component
public class InviteAddTicketsCosumer {

    private static final Integer ADD_TICKETS_EACH_FIVE_INVITE_TYPE = 5;
    private static final String ADD_TICKETS_EACH_FIVE_SET_KEY = "ADD_TICKETS_EACH_FIVE_SET_KEY_";
    public static Logger log = LoggerFactory.getLogger(InviteAddTicketsCosumer.class);

    @Autowired
    TouPiaoBiz touPiaoBiz;

    @Autowired
    RedisUtils redisUtils;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConstants.INVITE_ADD_TICKET_QUEUE, durable = "true"), exchange = @Exchange(value = MqConstants.INVITE_ADD_TICKET_EXCHANGE, durable = "true", type = "topic"), key = MqConstants.INVITE_ADD_TICKET_ROUTINGKEY))
    public void tasks(String msg) {
        log.info("[加票收到任务消息:{}]", msg);

        List<MemberVo> msgJsonList = null;
        Integer memberId = null;
        try {
            msgJsonList = GsonUtils.fromJson(msg, new TypeToken<List<MemberVo>>() {
            }.getType());
            memberId = msgJsonList.get(0).getInviter();
            long setCount = redisUtils.sSet(ADD_TICKETS_EACH_FIVE_SET_KEY + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")), memberId);
            if (setCount == 0) {
                return;
            }
        } catch (Exception e) {
            log.info(String.valueOf(e));
            return;
        }
        long count = msgJsonList.stream().filter(memberVo -> memberVo.getInviteesStatus() == 2).filter(memberVo -> {
            Timestamp t = memberVo.getRegTime();
            Date date = new Date(t.getTime());
            LocalDate localDate1 = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate localDate = LocalDate.now();
            //获取两个日期的天数差，为前一个减去后一个，正数则为前面的日期较晚
            int i = localDate.compareTo(localDate1);
            return i == 0;
        }).count();
        if (count > 4) {
            try {
                touPiaoBiz.addLottery(memberId, ADD_TICKETS_EACH_FIVE_INVITE_TYPE, Constants.toupiao_conf);
            } catch (Exception e) {
                log.error("[用户加票失败:]", e);
            }
        }
    }
}
